package com.kg.fiber.service;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.swing.AbstractListModel;
import javax.swing.table.AbstractTableModel;

import org.json.JSONObject;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.kg.fiber.domain.AppConfig;
import com.kg.fiber.domain.NetNode;

import lombok.Getter;

@Component
//@Log4j
public class NodeBoss {
	
	private List<NodeModal> modals = Lists.newLinkedList();
	
	@Getter
	private TableModel tableModel = new TableModel();
	
	@Getter
	private ListModel listModel = new ListModel();
	
	
	@PostConstruct
	private void postConstruct() {
		
		AppConfig config = AppConfig.instance.get();
		
		List<NetNode> nodes = config.getNodes();
		
		for(NetNode node : nodes) {
			
			NodeModal mm = new NodeModal(node);
			
			modals.add(mm);
			
		}
		
	}
	
	@PreDestroy
	private void preDestroy() {
		
		
		
	}
	
	private NodeModal get(String host) {
		
		for(NodeModal mod : modals) {
			
			if(mod.getNode().getHost().equals(host))
				return mod;
			
		}
		
		return null;
		
		
	}
	
	
	public void createNode(String host, String mark) throws RuntimeException {
		
		NodeModal mod = get(host);
		if(mod != null) {
			throw new RuntimeException("该IP地址已经被使用");
		}
		
		NetNode node = new NetNode();
		node.setHost(host);
		node.setMark(mark);
		
		AppConfig config = AppConfig.instance.get();
		config.getNodes().add(node);
		AppConfig.instance.set();
		
		NodeModal mm = new NodeModal(node);
		modals.add(mm);
		
		tableModel.fireTableDataChanged();
		
		listModel.fireContentsChanged(0, modals.size() - 1);
		
	}
	
	public void deleteNode(String host) {
		
		NodeModal mod = get(host);
		if(mod != null) {
			modals.remove(mod);
		}

		AppConfig config = AppConfig.instance.get();
		NetNode node = config.findNode(host);
		if(node != null) {
			config.getNodes().remove(node);
			AppConfig.instance.set();
			
			tableModel.fireTableDataChanged();
			
			listModel.fireContentsChanged(0, modals.size() - 1);
		}

	}
	
	public void dispatch(String host, int port, JSONObject value) {
		
		NodeModal mod = get(host);
		
		if(mod != null) {
			
			mod.dispatch(host, value);
			
			int idx = modals.indexOf(mod);
			
			tableModel.fireTableRowsUpdated(idx, idx);
			
			listModel.fireContentsChanged(idx, idx);
			
		}
		
	}
	
	class ListModel extends AbstractListModel<NodeModal> {

		/**
		 * 
		 */
		private static final long serialVersionUID = 8917173448583277916L;

		@Override
		public int getSize() {
			return modals.size();
		}

		@Override
		public NodeModal getElementAt(int index) {
			return modals.get(index);
		}
		
	    public void fireContentsChanged(int index0, int index1) {
	        this.fireContentsChanged(this, index0, index1);
	    }

	    public void fireIntervalAdded(Object source, int index0, int index1) {
	    	this.fireIntervalAdded(this, index0, index1);
	    }
	    
	    public void fireIntervalRemoved(Object source, int index0, int index1) {
	    	this.fireIntervalRemoved(this, index0, index1);
	    }

		
	}
	
	
	class TableModel extends AbstractTableModel {

		/**
		 * 
		 */
		private static final long serialVersionUID = -5118952655054018979L;

		@Override
		public int getColumnCount() {
			return 4;
		}
		
		@Override
		public String getColumnName(int column) {
			switch(column) {
			case 0: return "序号"; 
			case 1: return "主机"; 
			case 2: return "位置"; 
			case 3: return "状态";
			default: break;
			}
			return super.getColumnName(column);
		}
		
		@Override
		public int getRowCount() {
			return modals.size();
		}
		
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			
			try {
			
				NodeModal mod = modals.get(rowIndex);
				switch(columnIndex) {
				case 0:
					return rowIndex + 1;
					
				case 1:
					return mod.getNode().getHost();

				case 2:
					return mod.getNode().getMark();
					
				case 3:
					return mod.getState().getMark();
					
				default: return "";
				
				}
			
			} catch(Exception e) {
				
				return null;
			}
		}


		
		
		
	}
	

}
